package demo.practice.stack;

import java.util.Stack;

public class P682 {

    public int calPoints(String[] ops) {
        Stack<Integer> stack= new Stack<>();
        for(String s :ops){
           switch(s){
               case "C":
                   if(stack.size()>0)
                    stack.pop();
                   break;
               case "D":
                   if(stack.size()>0)
                       stack.add(stack.peek()*2);
                   break;
               case "+":
                   if(stack.size()>1){
                       Integer top = stack.pop();
                       int befor= stack.peek();
                       stack.add(top);
                       stack.add(top+befor);
                   } if(stack.size()==1)
                       stack.add(stack.peek());
                   break;
               default:
                   stack.add(Integer.parseInt(s));
           }
        }

        int sum=0;
        for(Integer i :stack){
            sum+=i;
        }
        return sum;
    }
}
